#include <stdio.h>
#include "mazelib.h"

static int FindPath(pointT pt, pointT path[], int steps);

#define MAX_PATH 36

int main() {
	pointT path[MAX_PATH];
	int i, len;
	//ReadMazeMap("../nosolution.maz");
	ReadMazeMap("../pathlen.maz");
	SetPauseTime(0.000005);

	len = FindPath(GetStartPosition(), path, MAX_PATH);
	if (len == 0) {
		printf("No solution exists.\n");
	} else {
		printf("The following path is a solution:\n");
		for (i = 0; i < len; i++) {
			printf(" (%d, %d)\n", path[i].x, path[i].y);
		}
	}
}

static int FindPath(pointT pt, pointT path[], int maxSize) {
	directionT dir;
	int length;

	if (OutsideMaze(pt)) {
		path[0] = pt;
		return 1;
	} else if (maxSize == 1 || IsMarked(pt)) {
		return 0;
	}

	MarkSquare(pt);
	for (dir = North; dir <= West; dir++)
		if ( (!WallExists(pt, dir)
			&& (length = FindPath(AdjacentPoint(pt, dir), path+1, maxSize-1)))
			&& length != 0) {
					length++;
					path[0] = pt;
					break;
				}
	return length;
}